/*
* This file is part of RoboDS (http://code.google.com/p/robods/)
* Copyright (C) 2007 Blakharaz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

#include "Robot.h"

namespace robo
{

    Robot::Robot(int robotnum)
    :   mRobotNumber(robotnum),
	    mDirection(BOTTOM),
	    mEnabled(true),
	    mMapPosition()
    {
	    mMapPosition.X = 0;
	    mMapPosition.Y = 0;
    }

    Robot::~Robot()
    {
    }

    void Robot::turnLeft()
    {
	    if (mDirection == LEFT)
	    {
		    mDirection = BOTTOM;
	    }
	    else if (mDirection == BOTTOM)
	    {
		    mDirection = RIGHT;
	    }
	    else if (mDirection == RIGHT)
	    {
		    mDirection = TOP;
	    }
	    else if (mDirection == TOP)
	    {
		    mDirection = LEFT;
	    }
	    update();
    }

    void Robot::turnRight()
    {
	    if (mDirection == LEFT)
	    {
		    mDirection = TOP;
	    }
	    else if (mDirection == BOTTOM)
	    {
		    mDirection = LEFT;
	    }
	    else if (mDirection == RIGHT)
	    {
		    mDirection = BOTTOM;
	    }
	    else if (mDirection == TOP)
	    {
		    mDirection = RIGHT;
	    }
	    update();
    }

    void Robot::turnAround()
    {
	    if (mDirection == LEFT)
	    {
		    mDirection = RIGHT;
	    }
	    else if (mDirection == BOTTOM)
	    {
		    mDirection = TOP;
	    }
	    else if (mDirection == RIGHT)
	    {
		    mDirection = LEFT;
	    }
	    else if (mDirection == TOP)
	    {
		    mDirection = BOTTOM;
	    }
	    update();
    }

    void Robot::setDirection(Direction direction)
    {
	    mDirection = direction;
	    update();
    }

    bool Robot::isEnabled() const
    {
	    return mEnabled;
    }

    void Robot::setEnabled(bool enabled)
    {
	    mEnabled = enabled;
    }

    void Robot::setMapPosition(unsigned short x, unsigned short y)
    {
	    mMapPosition.X = x;
	    mMapPosition.Y = y;
	    update();
    }

    Vector2 Robot::getMapPosition() const
    {
	    return mMapPosition;
    }

    Vector2 Robot::getMapPositionWhenMoving(short movement) const
    {
	    Vector2 delta;
	    delta.X = 0;
	    delta.Y = 0;

	    if (mDirection == LEFT)
	    {
		    delta.X = -1;
	    }
	    else if (mDirection == BOTTOM)
	    {
		    delta.Y = 1;
	    }
	    else if (mDirection == RIGHT)
	    {
		    delta.X = 1;
	    }
	    else if (mDirection == TOP)
	    {
		    delta.Y = -1;
	    }

	    Vector2 pos = mMapPosition;
	    pos.X += delta.X * movement;
	    pos.Y += delta.Y * movement;

	    return pos;
    }

}
